package com.quma.statistics.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.quma.statistics.ILogOutDao;
import com.quma.statistics.MysqlConnection;
import com.quma.statistics.entity.LogoutData;



public class LogOutDaoImpl implements ILogOutDao {
private Statement stm = MysqlConnection.getStatement();
private final String tableName = "loginout_stream";	
	@Override
	public void update(LogoutData data) {
		
//		String sql = "UPDATE loginout_stream SET logout_time=?, online_time=? WHERE id=?";
		
		
		
	}

	@Override
	public LogoutData getLogData(String uid) {
		createIfNotExist();
//		String sql = "SELECT * FROM loginout_stream WHERE account = ? AND logout_time = 0 ORDER BY login_time DESC";
//		List<LogoutData> data = (List<LogoutData>) getSimpleJdbcTemplate().query(
//				sql, new RowMapper() {
//
//					@Override
//					public Object mapRow(ResultSet rs, int rowNum)
//							throws SQLException {
//						LogoutData data = new LogoutData();
//						data.id = rs.getLong("id");
//						data.account = rs.getString("account");
//						data.level = rs.getInt("level");
//						data.client_ip = rs.getString("client_ip");
//						data.login_time = rs.getLong("login_time");
//						return data;
//					}
//				}, uid);
//		return data != null && data.size() > 0? data.get(0): null;
	return null;
	}

	@Override
	public void save(LogoutData data) {
		
		String sql = "INSERT INTO " +tableName + " (role_id, role_name, level,account,client_ip,login_time,logout_time,online_time) " +
				"VALUES('"+data.role_id+
				"','" + data.role_name + "',"
				+ "" +data.level + ","
				+ "'"+ data.account+"',"
				+ "'"+ data.client_ip+"',"
				+ "" +data.login_time + ","
				+ "" +data.logout_time + ","
				+ "" +data.online_time+""
				+ ") ON DUPLICATE KEY UPDATE login_time='" +data.login_time +"'";
		try {
			createIfNotExist();
			
			stm.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void createIfNotExist() {
		String sql = "CREATE TABLE IF NOT EXISTS  `" + tableName + "` ("
				  +"`id` int(11) unsigned NOT NULL AUTO_INCREMENT,"
				  +"`role_id` int(11) DEFAULT NULL,"
				  +"`role_name` varchar(50) DEFAULT NULL,"
				  +"`level` int(11) DEFAULT NULL,"
				  +"`account` varchar(50) DEFAULT NULL,"
				  +"`client_ip` varchar(20) DEFAULT NULL,"
				  +"`login_time` int(20) DEFAULT NULL,"
				  +"`logout_time` int(20) DEFAULT NULL,"
				  +"`online_time` int(20) DEFAULT NULL,"
				  +"PRIMARY KEY (`id`)"
				+") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		try {
			stm.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
}
